package com.smartrobot.smartrobot.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class User {

    String id;
    String pwd;
    String name;
    String phone;
    String head;
    String role;
    String sex;
    String qr_code;
    String address;
    String nickname;
    String age;
    String barberId;
    String barberName;
    String signature;
    boolean isLogin;
//    String storeId;
//    String storeName;


    public final String ID = "u_id"  ;             //  用户id
    public final String NAME = "u_name";           //    用户姓名
    public final String PHONE = "u_phone";          //      电话
    public final String PWD = "u_password";
    public final String HEAD = "u_head";            //     头像地址
    public final String ROLE = "u_role";            //     角色
    public final String SEX = "u_sex";            //    性别
    public final String QRCODE = "u_qr_code";       //       二维码
    public final String ADDRESS = "u_address";      //        地址
    public final String NICKNAME =  "u_nickname";    //         昵称
    public final String AGE = "u_age";     //  年龄
    public final String BARBERID = "u_belong_barber_id"; //    所属理发师id
    public final String BARBERNAME = "u_belong_barber_name"; //  所属理发师名字
    public final String SIGNATURE = "u_signature";
    public final String ISLOGIN = "u_islogin";   //是否第一次登录



//    public final String STOREID = "u_belong_store_id";    //  所属店铺id
//    public final String STORENAME = "u_belong_store_name";  //  所属店铺名称

    public static User instance = new User();


    private User(){

        id = "未填写";
        pwd = "未填写";
        name = "未填写";
        phone = "未填写";
        head = "未填写";
        role = "未填写";
        sex = "未填写";
        qr_code = "未填写";
        address = "未填写";
        nickname = "未填写";
        age = "未填写";
        barberId = "未填写";
        barberName = "未填写";
        signature = "未填写";
        isLogin = false;
//        storeId = "未填写";
//        storeName = "未填写";

    }


    public static User getInstance(){
        return instance;
    }




    public String getId() {
        return id;
    }




    public void setId(String id) {
        this.id = id;
    }




    public String getName() {
        return name;
    }




    public void setName(String name) {
        if(!name.equals("")&&name != null)
            this.name = name;
        else
            this.name = "未填写";
    }




    public String getPhone() {
        return phone;
    }




    public void setPhone(String phone) {
        if(!phone.equals("")&&phone != null)
            this.phone = phone;
        else
            this.phone = "未填写";
    }


    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {

        if(!signature.equals("")&&signature != null)
            this.signature = signature;
        else
            this.signature = "未填写";

    }

    public String getHead() {
        return head;
    }




    public void setHead(String head) {
        if(!head.equals("")&&head != null)
            this.head = head;
        else
            this.head = "未填写";
    }




    public String getRole() {
        return role;
    }




    public void setRole(String role) {
        if(!role.equals("")&&role != null)
            this.role = role;
        else
            this.role = "未填写";
    }




    public String getSex() {
        return sex;
    }




    public void setSex(String sex) {
        if(!sex.equals("")&&sex != null)
            this.sex = sex;
        else
            this.sex = "未填写";
    }




    public String getQr_code() {
        return qr_code;
    }




    public void setQr_code(String qr_code) {
        if(!qr_code.equals("")&&qr_code != null)
            this.qr_code = qr_code;
        else
            this.qr_code = "未填写";
    }




    public String getAddress() {
        return address;
    }




    public void setAddress(String address) {
        if(!address.equals("")&&address != null)
            this.address = address;
        else
            this.address = "未填写";
    }




    public String getNickname() {
        return nickname;
    }




    public void setNickname(String nickname) {
        if(!nickname.equals("")&&nickname != null)
            this.nickname = nickname;
        else
            this.nickname = "未填写";
    }




    public String getAge() {
        return age;
    }




    public void setAge(String age) {
        if(!age.equals("")&&age != null)
            this.age = age;
        else
            this.age = "未填写";
    }




    public String getBarberId() {
        return barberId;
    }




    public void setBarberId(String barberId) {
        if(!barberId.equals("")&&barberId != null)
        {
            this.barberId = barberId;
            System.out.println("写进去");
        }else
            this.barberId = "未填写";
    }




    public String getBarberName() {
        return barberName;
    }




    public void setBarberName(String barberName) {
        if(!barberName.equals("")&&barberName != null)
            this.barberName = barberName;
        else
            this.barberName = "未填写";
    }

    public boolean getIsLogin() {
        return isLogin;
    }

    public void setIsLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }


//    public String getStoreId() {
//        return storeId;
//    }



//
//    public void setStoreId(String storeId) {
//        if(!storeId.equals("")&&storeId != null)
//            this.storeId = storeId;
//    }
//
//
//
//
//    public String getStoreName() {
//        return storeName;
//    }
//
//
//
//
//    public void setStoreName(String storeName) {
//        if(!storeName.equals("")&&storeName != null)
//            this.storeName = storeName;
//    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


    public static boolean saveUserInfo(Context context){

        User user = User.getInstance();

        System.out.println("保存信息成功!"+user.toString());

        //只有这个程序才可以打开这个文件 保存用户信息
        SharedPreferences sharedPreferences = context.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();//获得编辑器
        editor.putString(user.ID, user.getId());
        editor.putString(user.NAME, user.getName());
        editor.putString(user.PHONE, user.getPhone());
        editor.putString(user.PWD, user.getPwd());
        editor.putString(user.HEAD, user.getHead());
        editor.putString(user.ROLE, user.getRole());
        editor.putString(user.SEX, user.getSex());
        editor.putString(user.QRCODE, user.getQr_code());
        editor.putString(user.ADDRESS, user.getAddress());
        editor.putString(user.NICKNAME, user.getNickname());
        editor.putString(user.AGE, user.getAge());
        editor.putString(user.BARBERID, user.getBarberId());
        editor.putString(user.BARBERNAME, user.getBarberName());
        editor.putBoolean(user.ISLOGIN, user.getIsLogin());

//        editor.putString(user.STOREID, user.getStoreId());
//        editor.putString(user.STORENAME, user.getStoreName());

        return editor.commit();//提交保存


    }



    public static boolean getUserInfo(Context context){
        //从文件中获取 用户信息 用作登陆使用
        SharedPreferences sharedPreferences = context.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        User user = User.getInstance();
        user.setId(sharedPreferences.getString(user.ID, ""));
        user.setName(sharedPreferences.getString(user.NAME, ""));
        user.setPhone(sharedPreferences.getString(user.PHONE, ""));
        user.setPwd(sharedPreferences.getString(user.PWD, ""));
        user.setHead(sharedPreferences.getString(user.HEAD, ""));
        user.setRole(sharedPreferences.getString(user.ROLE, ""));
        user.setSex(sharedPreferences.getString(user.SEX, ""));
        user.setQr_code(sharedPreferences.getString(user.QRCODE, ""));
        user.setAddress(sharedPreferences.getString(user.ADDRESS, ""));
        user.setNickname(sharedPreferences.getString(user.NICKNAME, ""));
        user.setAge(sharedPreferences.getString(user.AGE, ""));
        user.setBarberId(sharedPreferences.getString(user.BARBERID, ""));
        user.setBarberName(sharedPreferences.getString(user.BARBERNAME, ""));
        user.setIsLogin(sharedPreferences.getBoolean(user.ISLOGIN, false));
//        user.setStoreId(sharedPreferences.getString(user.STOREID, ""));
//        user.setStoreName(sharedPreferences.getString(user.STORENAME, ""));

        instance = user;

        if(user.getPhone().equals("未填写"))
            return false;
        return true;

    }

    public void reSert(){
        id = "未填写";
        pwd = "未填写";
        name = "未填写";
        phone = "未填写";
        head = "未填写";
        role = "未填写";
        sex = "未填写";
        qr_code = "未填写";
        address = "未填写";
        nickname = "未填写";
        age = "未填写";
        barberId = "未填写";
        barberName = "未填写";
        signature = "未填写";
        isLogin = false;
    }


    public String toString(){

        return id+" "+name+" "+id+" "+name+" "+phone+" "+head+" "+role+" "+address+" "+nickname+" "+age
                +" "+barberId+" "+barberName+" "+" "+phone+" "+head+" "+role+" "+sex
                +" "+qr_code + "  "+pwd+"  ";

    }







}
